package com.bignerdranch.android.expresshelper.Fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.bignerdranch.android.expresshelper.Activity.DialogActivity;
import com.bignerdranch.android.expresshelper.Activity.ResultActivity;
import com.bignerdranch.android.expresshelper.Bean.ExpressBean;
import com.bignerdranch.android.expresshelper.Dao.ExpressDao;
import com.bignerdranch.android.expresshelper.R;
import com.zxing.android.CaptureActivity;

/**
 * Created by Administrator on 2016/6/24.
 */
public class QueryFragment extends Fragment implements View.OnClickListener {
    private static final int REQUEST_CODE_SCAN = 1;

    private static final String DECODED_CONTENT_KEY = "codedContent";
    private static final String DECODED_BITMAP_KEY = "codedBitmap";

    private TextView mTopText;
    private Button mQuery;
//    private String url = "http://route.showapi.com/64-19?showapi_appid=20076&showapi_sign=b697ebeae51b4525b7ea856bade2d325";
    private EditText expressNum;
    private TextView expressName;
    private ImageButton clear;
    private ExpressDao mDao;
    private String simName;
    private ImageButton goods_name;
    private EditText diy_name;
    private ImageButton scanner;



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    /*    if (resultCode != Activity.RESULT_OK) {
            return;
        }
*/
        switch (requestCode){
            case 0:
                if (data == null) {
                    return;
                }
                simName = data.getStringExtra("simName");
                String name = data.getStringExtra("Name");
                expressName.setText(name);
                break;
            case 1:
                if (data != null) {
                    String content = data.getStringExtra(DECODED_CONTENT_KEY);

                    expressNum.setText( content);

                }

                break;
        }
       /* if (requestCode == 0) {

        }*/


    }


    @Override
    public void onResume() {
        super.onResume();
        if (!expressName.getText().toString().equals("")){
            clear.setVisibility(View.VISIBLE);
        }else {
            clear.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (!expressName.getText().toString().equals("")){
            clear.setVisibility(View.VISIBLE);
        }else {
            clear.setVisibility(View.INVISIBLE);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_query, container, false);

        initView(view);
        return view;
    }

    private void initView(View view) {
        mTopText = (TextView) view.findViewById(R.id.top_text);
        mTopText.setText(R.string.query);
        mQuery = (Button) view.findViewById(R.id.query_button);
        mQuery.setOnClickListener(this);

        expressNum = (EditText) view.findViewById(R.id.expressNum);
        expressNum.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                expressNum.setText("");
                expressNum.setHint("请输入运单号");
                return false;
            }
        });

        expressName = (TextView) view.findViewById(R.id.expressName);
        expressName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), DialogActivity.class);
                intent.putExtra("expressNum", expressNum.getText().toString().trim());
                startActivityForResult(intent, 0);

            }
        });



        clear = (ImageButton) view.findViewById(R.id.clear);
        clear.setOnClickListener(this);

        mDao = new ExpressDao(getActivity());
        goods_name = (ImageButton) view.findViewById(R.id.goods_name);
        goods_name.setOnClickListener(this);
        diy_name = (EditText) view.findViewById(R.id.diy_name);
        diy_name.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                diy_name.setText("");
                diy_name.setHint("备注物品名称(非必填)");
                return false;
            }
        });

        scanner = (ImageButton) view.findViewById(R.id.scanner);
        scanner.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.query_button:
                Log.e("--expressNum->>",expressNum.getText().toString()+"999");
                Log.e("--expressName->>",expressName.getText().toString()+"988");
                Log.e("--diy_name->>",diy_name.getText().toString()+"988");

                if (!expressNum.getText().toString().equals("") && !expressName.getText().toString().equals("")) {
                    ExpressBean bean = new ExpressBean();
                    bean.setExpressNum(expressNum.getText().toString().trim());
                    bean.setExpressName(expressName.getText().toString().trim());
                    bean.setExpressIcon(simName);
                    if (diy_name.getText().toString().equals("")){
                        bean.setExpressNick("我的快递");
                    }else {
                        bean.setExpressNick(diy_name.getText().toString());
                    }

                    if (mDao.query(expressNum.getText().toString().trim()) == null) {
                        mDao.insert(bean);

                    } else {
                        Toast.makeText(getContext(), "您已经查询过该记录", Toast.LENGTH_SHORT).show();
                    }

                    Intent i = new Intent(getActivity(), ResultActivity.class);
                    i.putExtra("num", expressNum.getText().toString().trim());
                    i.putExtra("name", simName);
                    startActivity(i);
                } else {
                    Toast.makeText(getContext(), "运单号或快递公司不能为空", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.clear:


                expressName.setText("");
                expressName.setHint(R.string.tip);

                    clear.setVisibility(View.INVISIBLE);

                break;

            case R.id.goods_name:
                diy_name.setFocusable(true);
                diy_name.requestFocus();
//                diy_name.setHint("");


                break;
            case R.id.scanner:
                Intent intent = new Intent(getActivity(),
                        CaptureActivity.class);
                //activity双向跳转
                startActivityForResult(intent, REQUEST_CODE_SCAN);


                break;
        }
    }





   /* private void submit() {
        // validate
        String name = diy_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(getContext(), "备注物品名称", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something


    }*/
}
